package com.xdl.flytest.service;

import com.xdl.flytest.bean.Interface;
import com.xdl.flytest.bean.InterfaceInfo;
import com.xdl.flytest.bean.Project;
import com.xdl.flytest.dao.InterfaceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 80736
 * @Date: 2018/12/30 15:47
 * @Description:
 */
@Service
public class InterfaceService {

    @Autowired
    private InterfaceMapper interfaceMapper;

    @Autowired
    private MongoTemplate mongoTemplate;


    public List<Interface> getInterfaces() {
        return interfaceMapper.selectAllInterfaces();
    }

    public void addInterface(Interface ife) {
        interfaceMapper.insertSelective(ife);
    }

    public Interface getInterfaceById(Integer interfaceId) {
//        return interfaceMapper.selectByPrimaryKeyWithProject(interfaceId);
        return interfaceMapper.selectByPrimaryKey(interfaceId);
    }

    public Interface getInterfaceByName(String interfaceName) {
        return interfaceMapper.selectByInterfaceName(interfaceName);
    }

    public void updateInterface(Interface in) {
        interfaceMapper.updateByPrimaryKeySelective(in);
    }

    public boolean deleteInterface(Integer interfaceId) {
        if (interfaceId != null) {
            interfaceMapper.deleteByPrimaryKey(interfaceId);
            return true;
        }

        throw new RuntimeException("param error");
    }

    public List<Project> getFromMongo() {
        Project project = new Project();
        project.setCreatorId(3);
        project.setDescription("test project");
        project.setProjectId(10034);
        project.setStatus("开发中");
        return mongoTemplate.find(new Query(Criteria.where("status").is("开发中")), Project.class, "flytest");

        //mongoTemplate.save(project, "flytest");
        //return mongoTemplate.findAll(Project.class, "flytest");
        //return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Project.class, "flytest");
        //return mongoTemplate.findOne(new Query(Criteria.where("status").is("开发中")), Project.class, "flytest");
    }

    public void addInterfaceToMongo(InterfaceInfo interfaceInfo) {
        mongoTemplate.insert(interfaceInfo);
    }

    public InterfaceInfo getInterfaceInfoFromMongo(String id) {

        return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), InterfaceInfo.class, "interfaceInfo");
    }

    public void updateInterfaceToMongo(InterfaceInfo interfaceInfo) {
        Query query = new Query(Criteria.where("_id").is(interfaceInfo.get_id()));
        Update update = new Update();
        update.set("type", interfaceInfo.getType());
        update.set("params", interfaceInfo.getParams());
        update.set("headers", interfaceInfo.getHeaders());
        update.set("body", interfaceInfo.getBody());

        mongoTemplate.updateFirst(query, update, InterfaceInfo.class);
    }

    public void removeInterfaceInfoFromMongoById(String id) {
        mongoTemplate.remove(new Query(Criteria.where("_id").is(id)), InterfaceInfo.class);
    }

    public List<Interface> getInterfaceByProjectId(Integer projectId) {
        return interfaceMapper.selectByProjectId(projectId);
    }
}
